//
//  LBWNetworkTools.swift
//  weibo
//
//  Created by Limonwei on 15/12/6.
//  Copyright © 2015年 heima. All rights reserved.
//

//抽象一个网络访问工具类

import UIKit
import AFNetworking

//定义一个枚举 请求方式
enum LBWRequestMethod: String {
    
    case GET = "GET"
    case POST = "POST"
    
}

//这个自定义网络工具类继承自AFHTTPSessionManager

class LBWNetworkTools: AFHTTPSessionManager {
    
    //给通过返回的闭包,定义一个别名
    typealias LBWNetworkToolCallBack = (response: AnyObject?, error: NSError?)->()
    
    //声明一个单例
    static let shareTool: LBWNetworkTools = {
        
        let tools = LBWNetworkTools()
        // 在 Swift 里面, 会把 NSSet 转成 Set
       tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools

    }()
    
    //发送请求
    func request(method:LBWRequestMethod, url: String, params: AnyObject?, callback:(response: AnyObject?, error: NSError?)->()){
        
        //定义成功请求之后的闭包
        let successCallback = { (dataTask: NSURLSessionDataTask, response: AnyObject) -> Void in
            callback(response:response, error: nil)
            
        }
        //定义失败请求之后的闭包
        let failureCallback = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            
            callback(response:nil, error: error)
            
        }
        
        //根据不同的请求调用不同的方法
        if method == .GET{
            self.GET(url, parameters: params, success: successCallback, failure: failureCallback)
        }else{
            self.POST(url, parameters: params, success: successCallback, failure: failureCallback)
        }
        
    }
    
    //提供上传文件的请求方法
    func requestUpload(ulrString: String, params: AnyObject?, data:NSData, name: String, callback: LBWNetworkToolCallBack) {
        
        //定义成功请求之后的闭包
        let succCallback = { (dataTask: NSURLSessionDataTask?, response: AnyObject) -> Void in
            
            callback(response: response, error: nil)
        }
        
        //定义失败请求的闭包
        let failedCallback = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            
            callback(response: nil, error: error)
        }
        
        POST(ulrString, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            // 在这个地方拼装 nsdata
            formData.appendPartWithFileData(data, name: name, fileName: "sss", mimeType: "application/octet-stream")
            }, success: succCallback, failure: failedCallback)
    }

}

//发送微博请求
extension LBWNetworkTools {
    //发送文字微博
    func updata(accessToken: String, status: String, callback: LBWNetworkToolCallBack) {
        //定义url 
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        //拼接参数
        let params = [
        
            "access_token": accessToken,
            "status": status

        ]
        //发送请求
        request(.POST, url: url, params: params, callback: callback)
    }
    
    //发送图片微博
    func upload(accessToken: String, status: String, image: UIImage, callback: LBWNetworkToolCallBack) {
        
        //定义url
         let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        //拼接参数
        let params = [
        
           "access_token": accessToken,
            "status": status
        ]
        //发送请求
        let data = UIImagePNGRepresentation(image)!
        requestUpload(url, params: params, data: data, name: "pic", callback: callback)
    }
}



//OAuth授权登陆的网络请求
extension LBWNetworkTools {
    
    //加载accessToken,并且把加载完成的数据回调回去
    func loadAccessToken(code: String, callback: LBWNetworkToolCallBack) {
        
        //请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //请求参数 拼接
        let params = [
            
            "client_id":WB_APPKEY,
            "client_secret":WB_APP_SECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":WB_REDIRECT_URI
        
        ]
        
        //发送请求
        request(.POST, url: urlString, params: params, callback: callback)
        
    }
    

    //请用户信息
    func loadUserInfo(uid: String, accessToken:String, callback: LBWNetworkToolCallBack) {
        
        //定义url
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        //组织参数
        let params = [
            
            "access_token": accessToken,
            
            "uid": uid
        
        ]
        //发送请求
        request(.GET, url: urlString, params: params, callback: callback)
        
    }
    
    
}


//首页微博数据请求
extension LBWNetworkTools {
    
    //基本信息 配图等
    func loadStatus(accessToken: String,sinceId: Int64 = 0,maxId: Int64 = 0, Callback:LBWNetworkToolCallBack){
        
        //请求地址
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        //请求参数
        let params = [
        
            "access_token": accessToken,
            
            //上拉加载参数
            "max_id": "\(maxId)",
            //下拉刷新参数
            "since_id": "\(sinceId)"
            
        ]
        
        request(.GET, url: urlString, params: params, callback: Callback)
        
    }
}


























